
interface StorageUtilsInterface<T> {
    set(key: string, value: T): void;
    get(key: string): T | null;
    remove(key: string): void;
}

class LocalStorageUtils<T> implements StorageUtilsInterface<T> {
    set(key: string, value: T): void {
        localStorage.setItem(key, JSON.stringify(value));
    }

    get(key: string): T | null {
        const item = localStorage.getItem(key);
        if (item) {
            try {
                return JSON.parse(item);
            } catch (error) {
                console.error("Error parsing stored value:", error);
            }
        }
        return null;
    }

    remove(key: string): void {
        localStorage.removeItem(key);
    }
}

class SessionStorageUtils<T> implements StorageUtilsInterface<T> {
    set(key: string, value: T): void {
        sessionStorage.setItem(key, JSON.stringify(value));
    }

    get(key: string): T | null {
        const item = sessionStorage.getItem(key);
        if (item) {
            try {
                return JSON.parse(item);
            } catch (error) {
                console.error("Error parsing stored value:", error);
            }
        }
        return null;
    }

    remove(key: string): void {
        sessionStorage.removeItem(key);
    }
}

// 使用时:
// let lsu = new LocalStorageUtils<string>();

export { LocalStorageUtils, SessionStorageUtils };